/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ifrr.gepro.util;

import java.util.Date;
import java.util.GregorianCalendar;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

/**
 *
 * @author paulo
 */
public class DataConverter implements Converter {

    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        try {
            int dia = Integer.valueOf(value.substring(0, 2));
            int mes = Integer.valueOf(value.substring(3, 5));
            int ano = Integer.valueOf(value.substring(6, 10));
            System.out.println("Dia:" + dia + ", Mes:" + mes + ", Ano:" + ano);
            return new GregorianCalendar(ano, mes, dia).getTime();
        } catch (Exception e) {
            JsfUtil.showMessageErro("Data inválida!");
            return null;
        }
    }

    public String getAsString(FacesContext context, UIComponent component, Object value) {
        try {
            return DateUtil.formataData((Date) value);
        } catch (Exception e) {
            JsfUtil.showMessageErro("Data inválida!");
            return null;
        }
    }
}
